package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Classname RentStartTimeRejectionHandler
 * @Description 起租时间，app甩单订购专用，非甩单订购不显示
 * @Author zhang.song
 * @Date 2021-09-28 19:49
 */
@Service
public class RentStartTimeRejectionHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttrVO) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = Optional.ofNullable(kiteSceneInst).map(KiteSceneInst::getServiceOfferId).orElse("");

        if(KiteStringUtils.isEqual(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER)) {
            String custId = KiteWebContext.getContext().getCust().getCustId();
            KiteInst kiteInst = querySceneInstService.getKiteInst(custId, kiteSceneInst.getRootInstType(), kiteSceneInst.getRootInstId());

            List<DcPublic> list = DcPublicCache.get("20210719001");
            // 是否五大双线落地方产品
            boolean flag = (list != null) && list.stream().anyMatch(dcPublic -> KiteStringUtils.isEqual(kiteInst.getSpecId(), dcPublic.getCodeb()));
            if(flag) {
                commonAttrVO.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);

                KiteAttr orderNatureAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.START_RENT_DATE);
                orderNatureAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                operateSceneInstService.setCommonAttr(sceneInstId, orderNatureAttr);

                setCommonAttr(commonAttrVO);
            }
        }
    }
}
